package com.servlet.book;

import com.alibaba.fastjson.JSONObject;
import com.enitiy.Book;
import com.enitiy.User;
import com.service.book.BookServiceImpl;
import com.service.user.UserServiceImpl;
import com.utils.model.Model;
import com.utils.page.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 创建作者 ：MySunshine
 * 创建时间 2022/10/28 9:14
 * 项目名称 SHUSHAN
 */
@WebServlet("/BookSelectAllByDba")
public class BookSelectAllBydba extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Page p=new Page();
        String pageNum = req.getParameter("page");
        System.out.println("当前页"+pageNum);
        if (pageNum==null){
            pageNum="1";
        }
        String pageSize = req.getParameter("limit");
        System.out.println( "每页条数"+pageSize);
        if (pageSize==null||pageSize.equals("")){
            pageSize="5";
        }
        p.setPageNum(Integer.parseInt(pageNum));
        p.setPageSize(Integer.parseInt(pageSize));

        //查询名字
        String key = req.getParameter("key");
        System.out.println(key);
        p.setName(key);

        BookServiceImpl bsi = new BookServiceImpl();
        List<Book> books = bsi.SelectBookAll(p);
        System.out.println(books);
        Model model = new Model();
        if (books!=null&&books.size()!=0){
            model.setCode(0);
            model.setData(books);
            model.setMsg("select  is  good!");
            model.setCount(p.getTotalCounts());
        }else {
            model.setCode(1);
            model.setMsg("select  is  not!");
        }
        System.out.println("查询成功！");
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().print(JSONObject.toJSON(model));
    }
}
